package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConst;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    JedisPool jedisPool;

    @Autowired
    SetmealDao setmealDao;

    @Transactional
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        //1. 添加套餐（回显主键）
        setmealDao.add(setmeal);
        //2. 维护套餐和检查组的关系
        if(setmeal.getId()!= null && checkgroupIds != null && checkgroupIds.length > 0){
            for (Integer checkgroupId : checkgroupIds) {
                setmealDao.addRelation(setmeal.getId(), checkgroupId);
            }
        }
        //更新redis
        List<Setmeal> setmealList = setmealDao.findAll();
        jedisPool.getResource().set("setmeal", JSON.toJSONString(setmealList));

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Setmeal> pageSetmeal = setmealDao.findByCondition(queryPageBean.getQueryString());
        return new PageResult(pageSetmeal.getTotal(),pageSetmeal.getResult());
    }

    @Override
    public List<Setmeal> findAll() {

        if (jedisPool.getResource().get("setmeal") == null){
            List<Setmeal> setmealList = setmealDao.findAll();
            jedisPool.getResource().set("setmeal", JSON.toJSONString(setmealList));

        }

        String setmeals = jedisPool.getResource().get("setmeal");
        List<Setmeal> setmealList = JSON.parseArray(setmeals,Setmeal.class);
        return setmealList;
    }


    @Override
    public Setmeal findDetailById(Integer id) {
        return setmealDao.findDetailById(id);
    }

    @Override
    public Setmeal findById(Integer id) {
        return setmealDao.findById(id);
    }

    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return setmealDao.findSetmealCount();
    }

    @Override
    public void edit(Integer[] checkgroupIds, Setmeal setmeal) {
        setmealDao.edit(setmeal);
        setmealDao.delRelation(setmeal.getId());
        //维护关系
        if (setmeal.getId() != null && checkgroupIds != null && checkgroupIds.length > 0){

            for (Integer checkgroupId : checkgroupIds) {
                setmealDao.set(setmeal.getId(),checkgroupId);
            }
        }

        //更新redis
        List<Setmeal> setmealList = setmealDao.findAll();
        jedisPool.getResource().set("setmeal", JSON.toJSONString(setmealList));



    }

    @Override
    public void delById(Integer id) {
        //删除关系
        setmealDao.delRelation(id);

        setmealDao.delById(id);
        //更新redis
        List<Setmeal> setmealList = setmealDao.findAll();
        jedisPool.getResource().set("setmeal", JSON.toJSONString(setmealList));
    }


}
